package com.hdd.utils;
import com.google.gson.Gson;
import com.hdd.config.Constant;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
@Slf4j
public class ImageUtil {
    static final String IMAGE_SUFFIX = "png";
    static Configuration cfg = new Configuration(Region.autoRegion());
    static  UploadManager uploadManager = new UploadManager(cfg);
    /**
     * @param posterImgUrl
     *            海报
     *            临时二维码
     * @param headImgUrl
     *            头像 nameText 昵称 cqText 二维码文字
     * @return 合成图片地址
     */
    public static String drawImage(String posterImgUrl,  byte[] bytes, String headImgUrl, String nameText,
                                   String cqText,String zp) throws IOException {


        int width = 600;
        int height = 900;
        BufferedImage bgBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// RGB形式
        Graphics2D bgBufImageGraphics = bgBufImage.createGraphics();
        bgBufImageGraphics.setBackground(Color.WHITE);// 设置背景色
        bgBufImageGraphics.clearRect(0, 0, width, height);// 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。

        // bgBufImageGraphics.setBackground(new Color(255,255,255));
        bgBufImageGraphics.setPaint(Color.BLACK);// 设置画笔,设置Paint属性
        Font font1 = new Font("宋体", Font.PLAIN, 20);
        Font font2 = new Font("宋体", Font.PLAIN, 22);
        bgBufImageGraphics.setFont(font2);
        // 抗锯齿
        bgBufImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        BufferedImage qrCodeImage = ImageIO.read(in);
       // BufferedImage qrCodeImage = ImageIO.read(new URL("https://mmbiz.qpic.cn/mmbiz_jpg/cVgP5bCElFhNSicyHulVLuDYQFPZLsIAx9DEg1jNJQ36ATINgqMDpicO1HNAwffYIMC8RkOXXk02uw3H5JY9bbGQ/0?wx_fmt=jpeg"));
        BufferedImage headImage = ImageIO.read(new URL(headImgUrl));
        BufferedImage zuoping = ImageIO.read(new URL(zp));
        BufferedImage shapeUrl = ImageIO.read(new URL("https://qiniu.17huaduoduo.com/hdd-black_shape.png"));


        System.out.println(zuoping.getWidth()+"/"+zuoping.getHeight());

        // 设置圆形头像
        BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D roundHeadGraphics = roundHeadImg.createGraphics();
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight());
        roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(),
                headImage.getHeight(), Transparency.TRANSLUCENT);
        roundHeadGraphics = roundHeadImg.createGraphics();


        // 使用 setRenderingHint 设置抗锯齿
        roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        roundHeadGraphics.setClip(shape);
        roundHeadGraphics.drawImage(headImage, 0, 0, null);
        roundHeadGraphics.dispose();
        int bgWidth = (zuoping.getWidth()/2)-95;
        int bgHeight = (zuoping.getHeight()/2)-10;
        bgBufImageGraphics.drawImage(posterBufImage, 0, 0, 620, 980, null);
        bgBufImageGraphics.drawImage(zuoping, 40, 50, bgWidth,bgHeight, null);
        bgBufImageGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
        bgBufImageGraphics.drawImage(shapeUrl, 40, (70+bgHeight)- shapeUrl.getHeight(), bgWidth, shapeUrl.getHeight()-20, null);
        bgBufImageGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        bgBufImageGraphics.drawImage(qrCodeImage, 480, 780, 90, 90, null);
        bgBufImageGraphics.drawImage(roundHeadImg, 60, (55+bgHeight)- shapeUrl.getHeight()+30, 80, 80, null);
        bgBufImageGraphics.drawString("画多多,与世界分享你的作品", 35, 800);
        bgBufImageGraphics.drawString("长按立即查看", 35, 830);
        bgBufImageGraphics.setPaint(Color.white);
        bgBufImageGraphics.setFont(font1);
        bgBufImageGraphics.drawString(nameText, 150, (50+bgHeight)- shapeUrl.getHeight()+65);
        bgBufImageGraphics.drawString(cqText, 150, (50+bgHeight)- shapeUrl.getHeight()+95);
        bgBufImageGraphics.dispose();

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(bgBufImage, IMAGE_SUFFIX, imgOut);
//        File targetFile = new File("D:/img");
//
//        if (!targetFile.exists()) {
//
//            targetFile.mkdirs();
//
//        }
//        FileOutputStream out = new FileOutputStream(targetFile +"/"+ "1.png");
//        out.write(bs.toByteArray());
//        out.flush();
//        out.close();
        Auth auth = Auth.create(Constant.ACCESS_KEY, Constant.SECRET_KEY);
        String upToken = auth.uploadToken(Constant.bucket_web);
        Response response = uploadManager.put(bs.toByteArray(),System.currentTimeMillis()+"",upToken);
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        System.out.println(putRet.key);
        System.out.println(putRet.hash);
        return "https://qiniu.17huaduoduo.com/"+putRet.key;// 返回合成的图片地址url
    }

}